﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Server
{
    public partial class ViewListProcesses : Form
    {
        public event FormExitEventHandler OnFormExit = null;
        public event KillProcessEventHandler OnKillProcess = null;
        private int id = 0;
        private int i;
        private int index = 0;
        private string name;
        public bool isReload = false;
        private bool isExist = false;

        public ViewListProcesses()
        {
            InitializeComponent();
        }

        public void ClearListProcesses()
        {
            this.id = 0;
            this.listView1.Items.Clear();
        }

        public List<string> GetListProcessesName()
        {
            List<string> list = new List<string>();
            for (int i = 0; i < this.listView1.Items.Count; i++)
            {
                list.Add(this.listView1.Items[i].SubItems[1].Text);
            }
            return list;
        }

        public void SetListProcesses(string id, string name, string memory)
        {
            try
            {
                if (!this.isReload)
                {
                    this.listView1.Items.Add(id);
                    this.listView1.Items[this.id].SubItems.Add(name);
                    this.listView1.Items[this.id].SubItems.Add(memory);
                    this.id++;
                }
                else
                {
                    for (i = 0; i < this.listView1.Items.Count; i++)
                    {
                        if (this.listView1.Items[i].SubItems[1].Text.ToLower().Equals(name.ToLower()))
                        {
                            this.isExist = true;
                            break;
                        }
                    }
                    if(!this.isExist)
                    {
                        this.listView1.Items.Add(id);
                        this.listView1.Items[this.id].SubItems.Add(name);
                        this.listView1.Items[this.id].SubItems.Add(memory);
                        this.id++;
                    }
                    this.isExist = false;
                }
            }
            catch
            {
            }
        }

        private void btnKill_Click(object sender, EventArgs e)
        {
            this.RemoveItem(this.index);
            this.id--;
            if (this.OnKillProcess != null)
            {
                this.OnKillProcess(this, new ProcessEventArgs(this.name));
            }
        }

        public void RemoveItem(int index)
        {
            this.listView1.Items.RemoveAt(index);
        }

        private void ViewListProcesses_FormClosed(object sender, FormClosedEventArgs e)
        {
            if (this.OnFormExit != null)
            {
                this.OnFormExit(this, new FormExitEvenArgs());
            }
        }

        private void listView1_MouseDown(object sender, MouseEventArgs e)
        {
            try
            {
                this.name = this.listView1.GetItemAt(e.X, e.Y).SubItems[1].Text;
                this.index = this.listView1.GetItemAt(e.X, e.Y).Index;
            }
            catch
            { }
        }
    }
}
